package com.yq.interview.jvm.study;

import java.util.HashMap;
import java.util.WeakHashMap;

/**
 * @ClassName WeakHashMapDemo
 * @Description WeakHashMap
 * @Author yaoqiang
 * @Date 2020/11/25 11:44
 * @Version 1.0
 */
public class WeakHashMapDemo {
    public static void main(String[] args) {
        myHashMap();
        System.out.println("============");
        myWeakHashMap();
    }

    private static void myWeakHashMap() {
        WeakHashMap<Integer,String> map=new WeakHashMap<>();
        Integer key=new Integer(2);
        String value="1212";
        map.put(key,value);
        System.out.println(map);//{2=1212}
        key=null;
        System.out.println(map);//{2=1212}
        System.gc();
        System.out.println(map);//{}

    }

    private static void myHashMap() {
        HashMap<Integer,String> map=new HashMap<>();
        Integer key=new Integer(1);
        String value="1212";
        map.put(key,value);
        System.out.println(map);
        key=null;
        System.out.println(map);
        System.gc();
        System.out.println(map);


    }
}
